//201902104003邵潇漩
package bysj.service;

import bysj.dao.ActorDao;
import bysj.dao.TeacherDao;
import bysj.dao.UserDao;
import bysj.domain.Department;
import bysj.domain.Teacher;
import bysj.domain.User;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class TeacherService {
	private static TeacherDao teacherDao= TeacherDao.getInstance();
	private static TeacherService teacherService=new TeacherService();
	private TeacherService(){}
	
	public static TeacherService getInstance(){
		return teacherService;
	}
	
	public Collection<Teacher> findAll(Pagination pagination, List<Condition> conditionList) throws SQLException {
		//获得连接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		Collection<Teacher> teachers = teacherDao.findAll(connection,conditionList,pagination);
		JdbcHelper.close(connection);
		return teachers;
	}
	
	public Teacher find(Integer id) throws SQLException {
		Connection connection= JdbcHelper.getConn();
		Teacher teacher=teacherDao.find(connection,id);
		JdbcHelper.close(connection);
		return teacher;
	}
	public boolean update(Teacher teacher)throws SQLException{
		Connection connection= JdbcHelper.getConn();
		boolean teacher_update=teacherDao.update(teacher,connection);
		JdbcHelper.close(connection);
		return teacher_update;
	}
	
	public boolean update(Teacher teacher,Connection connection) throws SQLException {
		return teacherDao.update(teacher,connection);
	}
	
	/*public boolean add(Teacher teacher) throws SQLException,BysjException{
		//获得进行事务的连接，添加教师和添加用户都使用本连接对象
		Connection connection= JdbcHelper.getConn();
		//将自动提交设为false，开始事务
		connection.setAutoCommit(false);
		boolean added=true;
		try{
			//teacherDao添加教师对象，返回值为自动生成的id
			int teacherId=teacherDao.add(teacher,connection);
			//为教师的id属性赋值
			teacher.setId(teacherId);
			//创建用户对象，密码和用户名取自teacher字段值no
			User user = new User(teacher.getNo(),teacher.getNo(),null,teacher);
			//增加用户
			int userId=UserDao.getInstance().add(user,connection);
			user.setId(userId);
			teacher.setUser(user);
			TeacherDao.getInstance().update(teacher, connection);
			added=UserDao.getInstance().update(user,connection);
			//提交事务
			connection.commit();
		}catch (SQLException e) {
			//回滚事务中的所有操作
			connection.rollback();
			//如果错误信息为Duplicate entry '01' for key 'username'，抛出自定义异常
			if (e.getMessage().equals("Duplicate entry '01' for key 'username'")) {
				throw new BysjException("新添加的教师的编号已经存在");
			}
		}finally {
			//将自动提交设置为false，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null,connection);
		}
		return added;
	}*/
	public boolean add(Teacher teacher) throws SQLException {
		//获得进行事务的连接，添加教师和添加用户都使用本连接对象
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false,开始事务
		connection.setAutoCommit(false);
		boolean added = false;
		try {
			//添加actor对象，返回值为自动生成的id
			int actorId = ActorDao.getInstance().add(connection);
			teacher.setId(actorId);
			//teacherDao添加教师对象，无返回值
			teacherDao.add(teacher, connection);
			//创建用户对象，密码和用户名取自teacher字段值no
			User user = new User(teacher.getNo(), teacher.getNo(), null, teacher);
			//增加用户
			added = UserDao.getInstance().add(user, connection);
			//提交事务
			connection.commit();
		} catch (SQLException e) {
			//回滚事务中所有操作
			e.printStackTrace();
			connection.rollback();
		} finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null, connection);
		}
		return added;
	}

	public boolean delete(Integer id) throws SQLException {
		Teacher teacher = this.find(id);
		return this.delete(teacher);
	}
	
	public boolean delete(Teacher teacher) throws SQLException {
		//如果该教师有关联的课题，不能删除
		//if(teacher.getProjects().size()>0){
		//	return false;
		//}
		Connection connection = JdbcHelper.getConn();
		boolean teacher_del=teacherDao.delete(connection,teacher);
		JdbcHelper.close(connection);
		return teacher_del;
	}
	public int countAll(Connection connection, Department department) throws SQLException{
		return teacherDao.countAll(department,connection);
	}
	//public static void main(String[] args)throws SQLException,BysjException{
		//Teacher teacher=new Teacher("苏同","03",2);
		//TeacherService.getInstance().add(teacher);
		//Teacher teacher1=new Teacher("张一鸣","02");
		//TeacherService.getInstance().add(teacher1);
		//System.out.println(TeacherDao.getInstance().findAll());

	//}
}
